<?php
/**
 * class HTML
 * @author pntrinh87@gmail.com
 * @version 2010-11-22
 */
class HTML
{
    var $title              = '';
    var $metakeywords       = '';
    var $metadescription    = '';
    var $domain             = '';
    var $titlesite          = '';
    var $year               = '2010';
    var $charset            = 'utf-8';
    var $subfolder          = '/';
    var $endline            = "";
    var $styles             = array();
    var $scripts            = array();
    var $arrUrl             = array();
    var $countUrl           = 0;    
    var $urlRoot            = '';
    var $category           = array();    
    var $js_folder          = 'javascript/';
    var $css_folder         = 'styles/';
    var $time               = 0;
   
    function HTML()
    {
        $this->time     = time();
    }
    /**
     * function setTitle($title)
     * @param $title: title of page
     */
    function setTitle($title)
    {
        $this->title  = $title;
    }
    
    /**
     * function setTitleSite($title)
     * 
     */
    function setTitleSite($title)
    {
        $this->titlesite = $title;
    }
    
    function setKeywords($keywords)
    {
        $this->metakeywords = $keywords;
    }
    
    function setDescription($description)
    {
        $this->metadescription = $description;
    }
    
    function setDomain($domain)
    {
        $this->domain = $domain;
    }
    
    function setSubfolder($folder)
    {
        $this->subfolder = $folder;
    }
    
    function setCharset($charset)
    {
        $this->charset = $charset;
    }
    
    function addCss($file)
    {
        if(!in_array($file,$this->styles))
        {
            $this->styles[] = $file;
        }        
    }
    
    function addJs($file)
    {
        if(!in_array($file,$this->scripts))
        {
            $this->scripts[] = $file;
        }        
    }
    
    function js_monify()
    {        
        $modify   = false;
        $file_time  = array();
        $new_file   = $this->time.'.js';      
        $data       = '';
        
        $old = $this->getConfig('jsvender_str',false,'all');
        $old = @json_decode($old);
        
        if($old != null)
        {
            $file_old = $this->js_folder.$old->file;
            $time_files = (array)$old->filetime;
        }
        
        foreach($this->scripts as $file)
        {
            $file_path = $this->js_folder.$file;
            if(is_file($file_path))
            {
                $file_time[$file] = @filemtime($file_path);
                if($file_time[$file] != $time_files[$file])
                {
                    $modify = true;
                }
            } 
        }
        
        if(is_file($file_old) && $modify == false)
        {
            return $file_old;
        }          
        
        foreach($this->scripts as $file)
        {
            $file_path = $this->js_folder.$file;
            if(is_file($file_path))
            {
                $data.= @file_get_contents($file_path);
                $data.= "\n";
            }            
        }
        
        ImportClass('jsmin'); 
        $data    = JSMin::minify($data);
        @file_put_contents($this->js_folder.$new_file,$data);
        
        
        $save_data = array(
            'file'  => $new_file,
            'time'  => $this->time,
            'filetime' => $file_time,
            'files' => $this->scripts
        );
        $save_data  = json_encode($save_data);
        $this->setConfig('jsvender_str',$save_data,'all');
        if(is_file($file_old))
        {
            @unlink($file_old);
        }
        
        return $this->js_folder.$new_file;
    }
    
    function css_monify()
    {
        $new_file   = $this->time.'.css';     
        $data       = '';        
        $modify     = false;
        $file_time  = array();
        
        $old        = $this->getConfig('cssvender_str',false,'all');
        $old        = @json_decode($old);
        
        if($old != null)
        {
            $file_old = $this->css_folder.$old->file;
            $time_files = (array)$old->filetime;
            
        }
        
        foreach($this->styles as $file)
        {
            $file_path = $this->css_folder.$file;
            if(is_file($file_path))
            {
                $file_time[$file] = @filemtime($file_path);
                if($file_time[$file] != $time_files[$file])
                {
                    $modify = true;
                }
            } 
        }
        
        if(is_file($file_old) && $modify ==false)
        {
            return $file_old;
        }
        //ImportClass('jsmin');   
        
        
        foreach($this->styles as $file)
        {
            $file_path = $this->css_folder.$file;
            if(is_file($file_path))
            {
                $data.= @file_get_contents($file_path);
                $data.= "\n";                
            } 
        }
        
        
        //$data    = JSMin::minify($data);
        
        @file_put_contents($this->css_folder.$new_file,$data);
        if(is_file($file_old))
        {
            @unlink($file_old);
        }
        $save_data = array(
            'file'  => $new_file,
            'time'  => $this->time,
            'filetime' => $file_time,
            'files' => $this->styles
        );
        $save_data  = json_encode($save_data);
        $this->setConfig('cssvender_str',$save_data,'all');
        return $this->css_folder.$new_file;
    }
    
    function script_for_layout($vender = false)
    {
        $html   = '';
        
        if($vender == true)
        {
            $js_file = $this->js_monify();
            $css_file= $this->css_monify();
            
            $html .= '<link rel="stylesheet" href="'.$this->subfolder.$css_file.'" type="text/css" media="screen" />'.$this->endline;
            
            $html .= '<script src="'.$this->subfolder.$js_file.'" type="text/javascript"></script>'.$this->endline;
            return $html;
        }
        //build html style css:
        foreach($this->styles as $file)
        {
            $html .= '<link rel="stylesheet" href="'.$this->subfolder.'styles/'.$file.'" type="text/css" media="screen" />'.$this->endline;
        }
        
        //build html javascript
        foreach($this->scripts as $file)
        {
            $html .= '<script src="'.$this->subfolder.'javascript/'.$file.'" type="text/javascript"></script>'.$this->endline;
        }
        return $html; 
    }
    
    function ParseUrl($url)
    {
        $url              = strtolower($url);
        $this->arrUrl     = explode("/",$url);
        $this->countUrl   = count($this->arrUrl);
        $this->urlRoot    = $this->arrUrl[0];
        
        if($this->countUrl>1 && empty($this->arrUrl[$this->countUrl-1]))
        {
            $this->countUrl-=1;
        }
    }
    
    function LoadData()
    {
        $this->category[0]  = $this->LoadCategory($this->arrUrl[0],0);
        $type               = strtolower($this->category[0]['cType']);
        
        for($i=1;$i<$this->countUrl;$i++)
        { 
            if($type=='news')
            {
                if($i==1 && strpos($this->arrUrl[$i],'.htm')===false)
                {
                    $this->category[$i]=$this->LoadNews($this->arrUrl[$i]);
                }else
                {
                    $this->category[$i] = $this->LoadCategory($this->arrUrl[$i],$this->category[$i-1]['iId']);
                } 
            }
            else if($type=="tourtype")
            {
                if($i==1)
                {
                    $this->category[$i]=$this->LoadTourType($this->arrUrl[$i]);    
                }else
                {
                    $this->category[$i]=$this->LoadTour($this->arrUrl[$i]);  
                }                
            }
            else if($type=="hotel")
            {
                if($this->arrUrl[1]=='inquiryhotel')
                {
                    $this->countUrl--;
                    $this->category[1]          = $this->LoadCategory($this->arrUrl[1],$this->category[$i-1]['iId']);
                    $this->category[1]['hotel']  = $this->LoadHotel($this->arrUrl[2]);                    
                    $this->setContent('type','inquiryhotel');
                }
                else if($i==1 && strpos($this->arrUrl[$i],'.htm')===false)
                {
                    $this->category[$i] = $this->LoadLocation($this->arrUrl[$i]);
                }else
                {
                    $this->category[$i] = $this->LoadHotel($this->arrUrl[$i]);   
                }
            }
            else if($type=="location")
            {
                $this->category[$i] = $this->LoadLocation($this->arrUrl[$i]);
            }
            else if($type=="newsletter")
            {
                if(strpos($this->arrUrl[$i],'sendfriend')!==false)
                {
                    $this->countUrl--;
                    $GLOBALS['actPost'] = 'sendfriend';                    
                }else
                {
                    $this->category[$i] = $this->LoadNewsLetter($this->arrUrl[$i]);
                }
            }
            else if($type=='customtourmap')
            {
                
            }
            else if($type=="tour")
            {
                if($this->arrUrl[1]=='inquiry')
                {
                    $this->countUrl--;
                    $this->category[1]          = $this->LoadCategory($this->arrUrl[1],$this->category[$i-1]['iId']);
                    $this->category[1]['tour']  = $this->LoadTour($this->arrUrl[2]);                    
                    $this->setContent('type','inquiry');
                }
                else if(isset($this->arrUrl[$i+1]) && strpos($this->arrUrl[$i+1],'book.htm')!==false)
                {
                    $this->countUrl--;
                    $this->category[$i] = $this->LoadTour($this->arrUrl[$i]);
                    $this->setContent('type','inquiry');
                }
                else if(strpos($this->arrUrl[$i],'.htm')!==false)
                {
                    $this->category[$i] = $this->LoadTour($this->arrUrl[$i]);
                }
                else
                {
                    $this->category[$i] = $this->LoadTourType($this->arrUrl[$i]);
                }                
            }
            else
            {     
                $this->category[$i] = $this->LoadCategory($this->arrUrl[$i],$this->category[$i-1]['iId']);
            }
        }  
        
        $this->setTitle($this->category[$this->countUrl-1]['cMetaTitle']);
        $this->setDescription($this->category[$this->countUrl-1]['cMetaDescription']);
        $this->setKeywords($this->category[$this->countUrl-1]['cMetaKeywords']);          
    }
    
    function LoadCategory($url,$idP=0)
    {
        global $dbsql,$language;
        
        $idP        = intval($idP);
        $sql_select = "select * from tblcategory where iParentId=$idP ";
        
        if(empty($url) && $idP == 0)
        {
            $sql_select .= " and cType = 'Home' ";
        }else
        {
            $sql_select .= " and cUrlString='$url' ";
        }
        $sql_select .=" and cLanguage='$language'";
        //echo $sql_select;
        return $dbsql->query_fetch($sql_select);
    }
    
    function LoadTourType($url)
    {
        global $dbsql,$language;
        
        $idP        = intval($idP);
        $sql_select = "select * from tbltourtype where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='tourtype';
        return $result;     
    }   
    
    function LoadTour($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblsettour where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='tour';
        return $result; 
    }
    
    function LoadNews($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblnews_content where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='news';
        return $result; 
    }
    
    function LoadNewsLetter($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblnewsletter where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='newsletter';
        return $result; 
    }
    
    function LoadLocation($url,$idP=0)
    {
        global $dbsql,$language;
        
        $idP        = intval($idP);
        if($url=="map")
        {
            return array("cMetaTitle"=>"Map","type"=>"map","cName"=>"Map");
        }
        $sql_select = "select * from tbllocation where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='location';
        return $result;        
    }
    
    function LoadHotel($url)
    {
        global $dbsql,$language;
        $url        = str_replace('.htm','',$url);
        $sql_select = "select * from tblhotel where cUrlString='$url'";
        $sql_select.=" and cLanguage='$language'";  
        $result     = $dbsql->query_fetch($sql_select);
        $result['type']='hotel';
        return $result;        
    }
    
    function getModule()
    {
        $module     = strtolower($this->category[0]['cType']); 
        if($this->category[0]==false)
        {
            $module = 'notfound';
        }
        else if (!empty($module) && is_dir("source/" . $module) == false)
        {
            $module = "index";
        }
        return $module;
    }
    
    function getUrl($i)
    {
        return $this->arrUrl[$i];
    }
    
    function getCategory()
    {
        return $this->category[$this->countUrl-1];
    }
    
    function getId()
    {
        return $this->category[$this->countUrl-1]['iId'];
    }
    
    function getContent($field,$json=null,$title=null)
    {
        if($json!=null)
        {
            @include_once("class_json.php");
            return FJSON::decode($this->category[$this->countUrl-1][$field]);
        }
        $text   = $this->category[$this->countUrl-1][$field];
        if($title!=null && !empty($text))
        {
            return $title.$text;
        }
        return ($text);
    }
    
    function checkContent($field)
    {
        $text   = strip_tags($this->category[$this->countUrl-1][$field]);
        if(empty($text))
        {
            return false;
        }
        return true;
    }
    
    function setContent($field,$value)
    {
        $this->category[$this->countUrl-1][$field] = $value;
    }
    
    function getField($i,$f)
    {
        $text   = $this->category[$i][$f];
        return $text;
    }
    
    function getTitle($field,$ttadd=null)
    {
        global $language;
        
        $ttadd  = (empty($ttadd))? '': ' '.$ttadd;
        $title  = $this->getContent($field).$ttadd;
        $html   = '<div class="title">';
        $html  .= '<h1>'.($title).'</h1>';
        $html  .= '</div>';
        return $html;
    }
    
    function getLinkHere()
    {
        $linkhere   = '<span id="linkhere">'.getLang("You are here: ");
        $split      = ' &raquo; ';
        $link       = '';
        for($i=0;$i<$this->countUrl;$i++)
        {
            $link       .= '/'.$this->category[$i]['cUrlString'];            
            if($i<$this->countUrl-1)
            {
                $linkhere   .= '<a class="phere" href="'.$link.'" title="'.$this->category[$i]['cName'].'">';
                $linkhere   .= $this->category[$i]['cName'].'</a>';
                $linkhere.= $split;
            }else
            {
                $linkhere   .= '<span>'.$this->category[$i]['cName'].'</span>';
            }
        }  
        $linkhere  .= '</span>';
        return $linkhere;
    }
    
    /**
     * function getHeader()
     * @return : html header 
     */
    function getHeader()
    {
        global $folder_path_root;
        
        $title  = (empty($this->title))? $this->titlesite : $this->title.' - '.$this->titlesite;
        $title  = ($title);
        $html   = '<title>'.$title.'</title>'.$this->endline;
        $html  .= '<meta name="description" content="'.$this->metadescription.'" />'.$this->endline;
        $html  .= '<meta name="keywords" content="'.$this->title.','.$this->metakeywords.'" />'.$this->endline;
        $html  .= '<meta name="copyright" content="'.$this->year.' '.$this->domain.'" />'.$this->endline;
        $html  .= '<meta name="owner" content="'.$this->domain.'"/>'.$this->endline;
        //$html  .= '<meta http-equiv="Reply-to" content="'.$this->domain.'" />'.$this->endline;
        $html  .= '<meta name="Robots" content="index, follow" />'.$this->endline;
        $html  .= '<meta http-equiv="Content-Type" content="text/html; charset='.$this->charset.'" />'.$this->endline;
              
             
        
        return $html;
    }
    
    function getBrowser()
    {
        $browser=strtolower($_SERVER['HTTP_USER_AGENT']);
        if(strpos($browser,"opera")!==false)
        {
        	$browser="opera";
        }
        else if(strpos($browser,"firefox")!==false)
        {
        	$browser="firefox";
        }
        else if(strpos($browser,"msie")!==false)
        {
        	$browser="ie";
        }
        else if(strpos($browser,"safari")!==false)
        {
        	$browser="safari";
        }
        return $browser;
    }
        
    function getBackgroundHome($height=1600,$time=7000)
    {
        if(!empty($this->urlRoot))
        {
            return;
        }
        
        $html   = '<div class="bghome fixed">';
        $html  .= '<script> ps=getSizePage();';
        $html  .= 'document.write(\'';
        $html  .= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="\'+ps.w+\'" height="'.$height.'" >';
        $html  .= '<param name="allowScriptAccess" value="sameDomain" />';
        $html  .= '<param name="movie" value="'.echoLink('/images/homepage.swf?timep='.$time.'&amp;file=http://'.$_SERVER['SERVER_NAME'].'/home.xml&amp;w_width=1024').'" />';
        $html  .= '<param name="quality" value="high" />';
        $html  .= '<param name="salign" value="lt" />';
        $html  .= '<param name="wmode" value="transparent"/>';
        $html  .= '<embed src="'.echoLink('images/homepage.swf').'" flashvars="timep='.$time.'&amp;file=http://'.$_SERVER['SERVER_NAME'].'/home.xml&amp;w_width=1024" wmode="transparent" quality="high" salign="lt"  width="\'+ps.w+\'" height="'.$height.'"  allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
        $html  .= '</object>\');</script></div>';
        return $html;
    }
    
    function getBackground()
    {
        global $folder_img_upload;
        $link   = $this->category[$this->countUrl-1]['cBackground'];
        if(empty($link))
        {
            $link   = echoLink("images/defaultbg.jpg");
        }
        else
        {
            $link   = echoLink($folder_img_upload.$link);    
        }
        return '<style type="text/css">body{background: #000 url('.$link.') no-repeat  fixed ;}</style>';
    }
    
    function getIntroImage()
    {
        global $folder_img_upload;
        $link   = $this->category[$this->countUrl-1]['cImage'];
        if(!empty($link))
        {
            return "/".$folder_img_upload.$link;
        }
        return false;
    }
    /**
     * function : getMenu($place=1,$split= ' | ')
     * @param $place: place in page
     * @param $split: split links
     */
    function getMenu($place=1,$split='')
    {
        global $dbsql,$language;
        
        $place      = intval($place);
        
        $sql_select = "select cUrlString,cName from tblcategory where iStatus=1 and (iPlace = $place or iPlace = 6) 
                    and cLanguage='$language' order by iOrder";
        $rs         = $dbsql->query($sql_select);
        $html       = '';
        $rows       = $dbsql->num_rows($rs);
        $i          = 0;
        while($result = $dbsql->fetch_array($rs))
        {
            $url    = (strtolower($result['cUrlString'])=="home")? "/" : "/".$result['cUrlString'];
            $class  = ($url=="/".$this->urlRoot)? "class='act'" : "";
            $html.= "<a $class href='".$url."'>".$result['cName']."</a>";
            $i++;
            if($i<$rows)
            {
                $html.='<span>'.$split.'</span>';
            }
        }
        return $html;
    }  
    /**
     * function : getMainMenu()
     * build menu drop down
     */
    function getMainMenu()
    {
        global $dbsql,$language;
        
        $sql_select = "select iId,cUrlString,cName,cType,cMetaTitle from tblcategory where iStatus=1 and (iPlace = 2 or iPlace=6) 
                        and cLanguage='$language' order by iOrder";
        $rs         = $dbsql->query($sql_select);
        
        $i          = 0;
        $mainMenu   = '<div><div id="chromemenu" class="chromestyle"><ul>'.$this->endline;
        $childMenu  = '';
        
        while($result = $dbsql->fetch_array($rs))
        {
            $i++; 
            $url    = (strtolower($result['cUrlString'])=="home")? "" : "/".$result['cUrlString'];
            $class  = ($url=="/".$this->urlRoot)? "class='permanentSelected'" : "";
            $mainMenu.= "<li><a $class rel=\"".$i."_subMenu\" href='".$url."/'>".htmlentities($result['cName'])."</a></li>".$this->endline;
                       
        }        
        $mainMenu  .= '</ul></div>';
        return $mainMenu;
    }
    
    function _BuildChildMenu($idP,$urlP,$type,$titleP,$i)
    {
        global $dbsql,$language;
        
        $type       = strtolower($type);
        $childMenu  = '';//  '<a href="'.$urlP.'/">'.getLang("View All").'</a>';   
        
        $sql_select = "select iId,cUrlString,cName from tblcategory where iStatus =1 
                         and iParentId =$idP ";
        if($type=="tour")
        {
            $sql_select = "select iId,cUrlString,cName from tbllocation where iParentId=0 ";
        }
        else if($type=="location" || $type=="hotel")
        {
            $sql_select = "select iId,cUrlString,cName from tbllocation where iStatus =1 and iParentId=0 ";
            if($type=='hotel')
            {
                $childMenu  = '<a href="'.$urlP.'">'.__('Search your favorite hotels/resorts').'</a>';
            }else
            {
                $childMenu  = '<a href="'.$urlP.'">'.$titleP.'</a>';
            }
        }        
        $sql_select.= " and cLanguage='$language' order by iOrder";
        
        $rs         = $dbsql->query($sql_select);
        
        if($dbsql->num_rows($rs)==0)
        {
            return;
        }       
        if($type=='tour')
        {
            $childMenu.= "<a href='".getUrlCatByType('tourtype')."'>".__('Search by Travel Style')."</a>";
        }     
        while($result   = $dbsql->fetch_array($rs))
        {            
            if($type=="tour")
            {
                $result['cName'].=" ".getLang("Journeys");
            }
            $childMenu.= "<a href='".$urlP."/".$result['cUrlString']."'>".htmlentities($result['cName'])."</a>";
        }
        
        return $childMenu;
    }
    /**
     * function : getConfig($name);
     * @name : name of config in db
     */
    function getConfig($name,$ent=false,$lang = null)
    {
        global $dbsql,$language;
        
        if(empty($lang))
        {
            $lang = $language;
        }
        $sql_select = "select cValue from tblconfig where cName = '$name' and cLanguage='$lang'";
        $result     = $dbsql->query_fetch($sql_select);
        if($ent==true)
        {
            $result['cValue'] = htmlentities($result['cValue']);
        }
        return $result['cValue'];
    }
    
    function setConfig($name,$value,$language = 'all')
    {
        global $dbsql;
        $sql_select = "select cValue from tblconfig where cName = '$name' and cLanguage='$language'";
        $result     = $dbsql->query($sql_select);
        if($dbsql->num_rows($result) > 0)
        {
            $sql_update = "update tblconfig set cValue = '$value' where cName='$name' and cLanguage = '$language'";
            return $dbsql->query($sql_update);
        }else
        {
            $sql_insert = "insert tblconfig (cName,cValue,cLanguage) values('$name','$value','$language')";
            return $dbsql->query($sql_insert);            
        }
    }
    
    function ListBanner()
    {
        global $dbsql,$language;
    
        $sql_select = "select * from tblbanner where cLanguage='$language' and iStatus =1 order by iOrder";
        return $dbsql->load($sql_select);
    }
}
?>